<?php
/**
 * Created by PhpStorm.
 * User: One
 * Date: 2018/2/5
 * Time: 10:11
 */
namespace Bstage\Controller;
use Common\Controller\BaseController;

class BstageController extends BaseController {

    public $admin_data;

    /**
     * 初始化方法
     */
    function _initialize(){
        $this->checkToken();
    }

    /**
     * 检查是否已经登录
     */
    protected function checkToken(){
        $token = $_REQUEST["token"]?$_REQUEST["token"]:$this->errorReturn("lack of token");

        $check_token = M("Admin")
            ->where(['token'=>$token])
            ->find();
        if(!$check_token){
            $this->returnJson(502,"token错误");
        }

        $this->admin_data = $check_token;
    }

    /**
     * 获取临时账号前缀
     * @return bool
     */
    public function generateTemporaryUsername(){
        $temporary_flag = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        $config_data = M("Config")
            ->where(['config_id'=>1])
            ->field("temporary_flag,temporary_flag2")
            ->find();
        $new_config_data = array();
        if($config_data['temporary_flag'] == "Z"){
            if($config_data['temporary_flag2'] == "Z"){
                $new_config_data['temporary_flag2'] = "A";
            }else{
                $flag_b = stripos($temporary_flag,$config_data['temporary_flag2']);
                $new_config_data['temporary_flag2'] = mb_substr($temporary_flag,$flag_b+1,1);
            }

            $new_config_data['temporary_flag'] = "A";
        }else{
            $flag_a = stripos($temporary_flag,$config_data['temporary_flag']);
            $new_config_data['temporary_flag'] = mb_substr($temporary_flag,$flag_a+1,1);

            $new_config_data['temporary_flag2'] = $config_data['temporary_flag2'];
        }
        $update_config = M("Config")
            ->where(['config_id'=>1,'temporary_flag'=>$config_data['temporary_flag'],'temporary_flag2'=>$config_data['temporary_flag2']])
            ->save($new_config_data);
        if($update_config){
            return $config_data['temporary_flag2'].$config_data['temporary_flag'];
        }else{
            return false;
        }
    }
}